<template>
  <div class="register">
    <div class="tabs">
      <router-link to="/login" class="tab">登录</router-link>
      <div class="tab active">注册</div>
    </div>
    <form @submit.prevent="handleRegister">
      <div class="form-group">
        <label>邮箱地址</label>
        <input v-model="email" type="email" required placeholder="请输入邮箱地址" />
      </div>
      <div class="form-group">
        <label>密码</label>
        <input v-model="password" type="password" required placeholder="请输入密码" />
      </div>
      <div class="form-group">
        <label>确认密码</label>
        <input v-model="confirmPassword" type="password" required placeholder="请再次输入密码" />
      </div>
      <div class="form-group">
        <label>昵称</label>
        <input v-model="nickname" type="text" required placeholder="请输入昵称" />
      </div>
      <div class="form-group verification-code">
        <label>验证码</label>
        <div class="code-input-container">
          <input v-model="verificationCode" type="text" required placeholder="请输入验证码" />
          <button type="button" class="send-code-btn" @click="sendVerificationCode">发送验证码</button>
        </div>
      </div>
      <button type="submit" class="register-btn">注册</button>
    </form>
    <p class="form-footer">已有账号？<router-link to="/login">立即登录</router-link></p>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { useRouter } from 'vue-router';

export default defineComponent({
  name: 'RegisterView',
  setup() {
    const router = useRouter();
    const email = ref('');
    const password = ref('');
    const confirmPassword = ref('');
    const nickname = ref('');
    const verificationCode = ref('');
    const codeSent = ref(false);
    const countdown = ref(0);
    
    const sendVerificationCode = () => {
      if (email.value && !codeSent.value) {
        // 发送验证码逻辑
        console.log('发送验证码到:', email.value);
        codeSent.value = true;
        countdown.value = 60;
        
        const timer = setInterval(() => {
          countdown.value--;
          if (countdown.value <= 0) {
            clearInterval(timer);
            codeSent.value = false;
          }
        }, 1000);
      }
    };
    
    const handleRegister = () => {
      // 验证密码是否一致
      if (password.value !== confirmPassword.value) {
        alert('两次输入的密码不一致');
        return;
      }
      
      // 注册逻辑
      console.log('注册:', email.value, nickname.value);
      localStorage.setItem('isLoggedIn', 'true');
      localStorage.setItem('userEmail', email.value);
      localStorage.setItem('userNickname', nickname.value);
      router.push('/');
    };
    
    return {
      email,
      password,
      confirmPassword,
      nickname,
      verificationCode,
      codeSent,
      countdown,
      sendVerificationCode,
      handleRegister
    };
  }
});
</script>

<style scoped>
.register {
  max-width: 400px;
  margin: 0 auto;
  padding: 2rem;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.tabs {
  display: flex;
  margin-bottom: 2rem;
  border-bottom: 1px solid #eee;
}

.tab {
  padding: 0.75rem 1.5rem;
  cursor: pointer;
  color: #666;
  text-decoration: none;
  font-weight: 500;
}

.tab.active {
  color: #007acc;
  border-bottom: 2px solid #007acc;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: #333;
}

.form-group input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

.verification-code .code-input-container {
  display: flex;
  gap: 10px;
}

.verification-code input {
  flex: 1;
}

.send-code-btn {
  white-space: nowrap;
  background-color: #f0f0f0;
  color: #333;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 1rem;
  cursor: pointer;
  transition: background-color 0.3s;
}

.send-code-btn:hover {
  background-color: #e0e0e0;
}

.register-btn {
  width: 100%;
  padding: 0.75rem;
  margin-top: 1rem;
  background-color: #007acc;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s;
}

.register-btn:hover {
  background-color: #0062a3;
}

.form-footer {
  margin-top: 1.5rem;
  text-align: center;
  color: #666;
}
</style>